package com.crocodile.game17.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;

import java.util.HashMap;
import java.util.Map;

public class JwtUtilN {

   public static String  createJwt(String subject){
       DateTime now = DateTime.now();
       DateTime newTime = now.offsetNew(DateField.MINUTE, 10);

       Map<String,Object> payload = new HashMap<>();
       //签发时间
       payload.put(JWTPayload.ISSUED_AT, now);
       //过期时间
       payload.put(JWTPayload.EXPIRES_AT, newTime);
       //生效时间
       payload.put(JWTPayload.NOT_BEFORE, now);
       //载荷
      payload.put("subject",subject);

       String key = "crocodile";
       return JWTUtil.createToken(payload, key.getBytes());
   }


   public static String parseJwt(String token) throws Exception{
       JWT jwt = JWT.of(token);

// JWT
       jwt.getHeader(JWTHeader.TYPE);
// HS256
       jwt.getHeader(JWTHeader.ALGORITHM);

// 1234567890
       String subject = (String) jwt.getPayload("subject");

       return subject;

   }

}
